package cn.iocoder.yudao.module.ems.service.supplier;

import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.SupplierListReqVO;
import cn.iocoder.yudao.module.ems.convert.supplier.ContactConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.spares.ContactDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.supplier.SupplierDO;
import cn.iocoder.yudao.module.ems.dal.mysql.supplier.ContactMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.ems.controller.admin.supplier.vo.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.ems.convert.supplier.SupplierConvert;
import cn.iocoder.yudao.module.ems.dal.mysql.supplier.SupplierMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.*;

/**
 * 供应商 Service 实现类
 *
 * @author 左达成
 */
@Service
@Validated
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    @Resource
    private ContactMapper contactMapper;

    @Override
    public Long createSupplier(SupplierCreateReqVO createReqVO) {
        // 插入
        SupplierDO supplier = SupplierConvert.INSTANCE.convert(createReqVO);
        supplierMapper.insert(supplier);
        Collection<ContactCreateReqVO> InfoList = createReqVO.getContact();
        for (ContactCreateReqVO info: InfoList) {
            info.setSupplierId(supplier.getId());
            contactMapper.insert(ContactConvert.INSTANCE.convert4(info));
        }
        // 返回
        return supplier.getId();
    }

    @Override
    public void updateSupplier(SupplierUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateSupplierExists(updateReqVO.getId());
        // 更新
        SupplierDO updateObj = SupplierConvert.INSTANCE.convert(updateReqVO);
        // 联系人信息
        Collection<ContactRespVO> InfoList = updateReqVO.getContact();
        for (ContactRespVO info: InfoList) {
            info.setSupplierId(updateReqVO.getId());
            if(info.getId() != null) {
                contactMapper.updateById(ContactConvert.INSTANCE.convert3(info));
            } else {
                contactMapper.insert(ContactConvert.INSTANCE.convert3(info));
            }
        }
        supplierMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplier(Long id) {
        // 校验存在
        this.validateSupplierExists(id);
        SupplierDO w = supplierMapper.selectById(id);
        List<ContactDO> infoList = contactMapper.selectList(id);
        for(ContactDO info : infoList) {
            contactMapper.deleteById(info.getId());
        }
        // 删除
        supplierMapper.deleteById(id);
    }

    private void validateSupplierExists(Long id) {
        if (supplierMapper.selectById(id) == null) {
            throw exception(SUPPLIER_NOT_EXISTS);
        }
    }

    @Override
    public SupplierDO getSupplier(Long id) {
        return supplierMapper.selectById(id);
    }

    @Override
    public List<SupplierDO> getSupplierList(Collection<Long> ids) {
        return supplierMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<SupplierDO> getSupplierPage(SupplierPageReqVO pageReqVO) {
        return supplierMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SupplierDO> getSupplierList(SupplierExportReqVO exportReqVO) {
        return supplierMapper.selectList(exportReqVO);
    }

    @Override
    public List<SupplierDO> getSimpleSuppliers(SupplierListReqVO reqVO) {
        return supplierMapper.selectList(reqVO);
    }

    @Override
    public List<SupplierDO> getSimpleSuppliers(Collection<Long> ids) {
        return supplierMapper.selectBatchIds(ids);
    }

}
